package designs.models.template.example.two;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.security.auth.kerberos.KerberosKey;


public abstract class OperationDatabase {
	Connection connection;
	Statement sql;
	ResultSet result;
	String dataBase,tableName;
	public OperationDatabase(String dataBase,String tableName) {
		this.dataBase = dataBase;
		this.tableName = tableName;
	}
	
	public final void lookResult() {
		loadDriver();
		createConnection();
		createStatement();
		handleResult();
	}
	
	public abstract void loadDriver();
	public abstract void createConnection();
	public final void createStatement() {
		try {
			sql = connection.createStatement();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public final void handleResult() {
		try {
			DatabaseMetaData metaData = connection.getMetaData();
			ResultSet columns = metaData.getColumns(null, null, tableName, null);
			int filedCount = 0;
			while(result.next()) {
				filedCount ++;
				System.out.printf("% - 15s", result.getString(4));
			}
			System.out.println();
			result = sql.executeQuery("SELECT * FROM "+ tableName);
			while(result.next()) {
				for (int i = 0; i < filedCount; i++) {
					System.out.printf(" % - 15s",result.getString(i));
				}
				System.out.println();
			}
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
